const {ccclass, property} = cc._decorator;

@ccclass
export default class Theme extends cc.Component {

    // 属性装饰器：可以在属性面板上显示
    // @property(类型)
    // 变量名:类型 = 值;
    
    //数组:@property([类型])
    // 数组名:类型[] = []

    private pageView:cc.PageView = null;                //翻页容器
    private leftBtn:cc.Button = null;                   //上一页按钮
    private rightBtn:cc.Button = null;                  //下一页按钮
    private toggleContainer:cc.ToggleContainer = null   //单选按钮组
    private toggleItems = []                            //用于存放每个单选按钮

    private index = 1;                                  //当前页面的索引值
    private totalIndex = 1;                             //总页数
    
    onLoad(){
        this.pageView = this.getComponentInChildren(cc.PageView)
        this.leftBtn = this.node.getChildByName("upPageBtn").getComponent(cc.Button)
        this.rightBtn = this.node.getChildByName("downPageBtn").getComponent(cc.Button)
        this.toggleContainer = this.getComponentInChildren(cc.ToggleContainer)
    }

    //初始化时调用
    start () {
        //获取当前pageView的页面索引值,给Index赋值
        this.index = this.pageView.getCurrentPageIndex()
        this.totalIndex = this.pageView.getPages().length
        this.toggleItems = this.toggleContainer.toggleItems
    }

    update (dt) {
        

        //判断左边按钮边界
        if(this.index <= 0){
            this.leftBtn.node.active = false
        }else{
            this.leftBtn.node.active = true
        }

        //判断右边按钮边界
        if(this.index >= this.totalIndex - 1){
            this.rightBtn.node.active = false
        }else{
            this.rightBtn.node.active = true
        }
    }

    onBackHomeClick(){
        cc.director.loadScene("startScene")
    }

    //左边按钮被点击时
    onLeftBtnClick(){
        this.index--;
        if(this.index >= 0){
            this.pageView.setCurrentPageIndex(this.index)
        }else{
            this.index = 0
        }
        this.toggleItems[this.index].isChecked = true
    }

    //右边按钮被点击时
    onRightBtnCLick(){
        
        this.index++;
        if(this.index < this.totalIndex){
            this.pageView.setCurrentPageIndex(this.index)
        }else{
            this.index = this.totalIndex
        }
        this.toggleItems[this.index].isChecked = true
    }

    onPageViewListenEvent(){
        this.index = this.pageView.getCurrentPageIndex()
        this.toggleItems[this.index].isChecked = true
    }

    onStagesClick(){
        cc.director.loadScene("stagesScene")
    }
}
